package web.servlet;

import domain.Product;
import persistence.ProductDao;
import persistence.impl.ProductDaoImpl;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ShowInformServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("showInforServlet。。。。。。。。。。。。。。。。。。");
        String categoryId = req.getParameter("categoryId");
        System.out.println("categotyId="+categoryId);
        ProductDao productDao = new ProductDaoImpl();
        List<Product> productList;
        switch (categoryId) {
            case "cats":
                productList = productDao.getProductListByCategory("CATS");
                break;
            case "dogs":
                productList = productDao.getProductListByCategory("DOGS");
                break;
            case "reptiles":
                productList = productDao.getProductListByCategory("REPTILES");
                break;
            case "birds":
                productList = productDao.getProductListByCategory("BIRDS");
                break;
            default:
                productList = productDao.getProductListByCategory("FISH");
                break;
        }
        String response = "ProductID            Name\n";
        int i = 0 ;
        for(;i<productList.size();i++)
        {
            Product product = productList.get(i);
            response+=product.getProductId()+"        "+product.getName()+"\n";

        }

        String shan1="<h2>";
        response=response.replace(shan1, "");
        String shan2="</h2>";
        response=response.replace(shan2, "");
        System.out.println(response);
        resp.setContentType("text/xml");
        PrintWriter out = resp.getWriter();
        out.write(response);
    }
    @Override
    protected void doPost(HttpServletRequest request , HttpServletResponse response) throws IOException {
        doGet(request,response);
    }
}
